//
//  SNCommentTextView.m
//  DayDayHappy
//
//  Created by yutao on 14-3-5.
//  Copyright (c) 2014年 yutao. All rights reserved.
//

#import "SNCommentTextView.h"
#import "GTMBase64.h"

#define                 kAlphaStartValue 0.5
#define                 kAlphaEndValue 0.0
#define                 kMaskAlphaStartValue                   0.5
@interface SNCommentTextView()
{
    UIView *_maskView;
    UITextView *_textInputView;
    BOOL    _showTextView;
    BOOL    _keyboardShowing;
}
@end

@implementation SNCommentTextView
@synthesize  delegate = delegate;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        _showTextView = NO;
        _keyboardShowing = NO;
        [self initSubviews];
        [self initNotification];
    }
    return self;
}

- (void)initSubviews
{
    _maskView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kDeviceWidth, kDeviceHeight)];
    _maskView.backgroundColor = [UIColor blackColor];
    _maskView.alpha = kAlphaEndValue;

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
    [_maskView addGestureRecognizer:tap];
    
    
    UILabel *titleLable = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, 280, 40)];
    [titleLable setText:@"写评论"];
    [titleLable setTextColor:kHappTextClolr];
    titleLable.textAlignment = NSTextAlignmentCenter;
    [self addSubview:titleLable];
    
    UIButton *sendButton = [[UIButton alloc] initWithFrame:CGRectMake(255 , 0, 50, 40)];
    [sendButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [sendButton addTarget:self action:@selector(sendButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [sendButton setTitle:@"发送" forState:UIControlStateNormal];
    [self addSubview:sendButton];
    
    
    _textInputView = [[UITextView alloc] initWithFrame:CGRectMake(20, 40, 280, 80)];
    _textInputView.layer.borderWidth = 0.5;
    _textInputView.layer.borderColor = kHappTextClolr.CGColor;
    [self addSubview:_textInputView];
    
}

- (void)sendButtonAction
{
    if ([self.delegate respondsToSelector:@selector(sendButtonDidClickedWithText:)]) {
        NSData *inputTextData = [_textInputView.text dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
        NSData *encodeData =   [GTMBase64 encodeData:inputTextData];
        
        NSString *inputText = [[NSString alloc] initWithData:encodeData encoding:NSUTF8StringEncoding];
        
        [self.delegate sendButtonDidClickedWithText:inputText];
    }
    
}


- (void)initNotification
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];
}

- (void)show
{
    if (_showTextView) {
        return;
    }
    
    _showTextView = YES;
    [self setTop:kDeviceHeight];
    
    if (_maskView.superview == nil) {
        [kRooViewController.view addSubview:_maskView];
    }
    
    if (self.superview == nil) {
        [kRooViewController.view addSubview:self];
    }
    
    [_textInputView becomeFirstResponder];
}

- (void)hide
{
    _showTextView = NO;
    _keyboardShowing = NO;
    [_textInputView resignFirstResponder];
}


- (void)setTop:(CGFloat)top
{
    CGRect frame = self.frame;
    frame.origin.y = top;
    self.frame = frame;
}

- (void)setBottom:(CGFloat)bottom
{
    CGRect frame = self.frame;
    frame.origin.y = bottom - CGRectGetHeight(frame);
    self.frame = frame;
}

- (void)keyboardWillShow:(NSNotification *)note
{
    NSDictionary  * keyBoardInfo = [note userInfo];
    
    if (_showTextView == NO) {
        return;
    }
    
    self.hidden = NO;
    
    CGRect keyboardFrame = [[note.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGFloat bottom = CGRectGetMinY(keyboardFrame);
    
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) {
        bottom += 20;
    }
    UIViewAnimationCurve animationCurve = [keyBoardInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];
    CGFloat duration = [keyBoardInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
    if (_keyboardShowing == NO) {
        [UIView animateWithDuration:duration delay:0.0 options:(animationCurve << 16) animations:^{
            [self setBottom:bottom];
            _maskView.alpha = kMaskAlphaStartValue;
        } completion:nil];
    }
    else {
        [self setBottom:bottom];
        _maskView.alpha = kMaskAlphaStartValue;
    }
    
    _keyboardShowing = YES;

}

- (void)keyboardWillHide:(NSNotification *)note
{
    _showTextView = NO;
    _keyboardShowing = NO;
    [_textInputView resignFirstResponder];
    
    NSDictionary  * keyBoardInfo = [note userInfo];
    UIViewAnimationCurve animationCurve = [keyBoardInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];
    CGFloat duration = [keyBoardInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
    [UIView animateWithDuration:duration delay:0.0 options:(animationCurve << 16) animations:^{
        [self setTop:kDeviceHeight];
        _maskView.alpha = kAlphaEndValue;
    } completion:nil];
}

- (void)keyboardDidHide:(NSNotification *)noti
{
    _showTextView = NO;
    _keyboardShowing = NO;
    [_textInputView resignFirstResponder];
}


#pragma mark -

- (void)handleGesture:(UIGestureRecognizer *)gesture
{
    if ([gesture isKindOfClass:[UITapGestureRecognizer class]]
        || ([gesture isKindOfClass:[UIPanGestureRecognizer class]] && gesture.state == UIGestureRecognizerStateBegan)) {
//        if ([self.delegate respondsToSelector:@selector(textViewWillHide:)]) {
//            [self.delegate textViewWillHide:self];
//        }
        
        [self hide];
    }
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
